(function($){ 
	$(function(){ 
		var propertyParser = function(property){
			var prop={};
			var func = /([a-zA-Z\d_-]+\s*):\s*function\s*\(([^\)]*)\)\s*\{((?:(?!\},)[\s\S])+)\}(,|$)/g;
			var str = /([a-zA-Z\d_-]+\s*):\s*['"]([^'":]+)['"](,|$)/g;
			var obj = /([a-zA-Z\d_-]+\s*):\s*(\{[^\}]+\})(,|$)/g;
			property.replace(func,function(s0,s1,s2,s3){ 
				var thisPara = s2 ? s2.replace(/\s*,\s*/g,",").trim() : ''; 
				s3 = s3 && s3.trim(); 
				if(s3){    
					prop[s1] = eval("(function(){ return function("+thisPara+"){"+s3+"}})()");
			 	}  
			});  
			property.replace(str,function(s0,s1,s2){
				s2 = s2.trim();
				s1 = s1.trim(); 
				if(/^\d+$/g.test(s2)){
					prop[s1] = Number(s2);
				}else if(s2 === "true"){
					prop[s1] = true;
				}else if(s2 === "false"){
					prop[s1] = false;
				}else{
				 	prop[s1] = s2;
				}
			}); 

			property.replace(obj,function(s0,s1,s2){
				var val = {}; 
				s2.replace(/(?:'|")?([a-zA-Z\d_-]+)(?:'|")?\s*:\s*(?:'|")?([^'":\{\}]+)(?:'|")?(?:,|\})/g,function(ss0,ss1,ss2){
					ss2 = ss2.trim();
					if(/^\d+$/g.test(ss2)){
						val[ss1] = Number(ss2);
					}else if(ss2 === "true"){
						val[ss1] = true;
					}else if(ss2 === "false"){
						val[ss1] = false;
					}else{
						val[ss1] = ss2; 
					} 
				});
				prop[s1] = val;
			}); 
			return prop;
		};

		$("form.validate-form").each(function(){  
			var options = {},_options = $(this).attr("validate-options");
			try{
				if(_options) options = propertyParser(_options);
			}catch(e){
				console.error(e);
				throw new Error("error validate-options value:" + _options,this);
			}  
			//遍历生成对象
			var _rules = {};
			var _messages = {};
			$(this).find("input,textarea,select").each(function(){
				 var _opt = $(this).attr("validate-options");
				 if(_opt && $(this).attr("name")){
				 	//判定是需要验证元素
				 	try{
				 		var opt = propertyParser(_opt);
				 		if(opt["rules"]){ 
				 			if(opt["rules"] == "required"){
				 				_rules[$(this).attr("name")] = "required";
				 			}else{
				 				_rules[$(this).attr("name")] =  opt["rules"];
				 			} 
				 		} 
				 		if (opt["messages"]) {
				 			_messages[$(this).attr("name")] =  opt["messages"];
				 		}
				 	}catch(e1){
				 		console.error(e1,"error validate-options value:" + _opt,this);
				 	}
				 }
			}); 
			//----------------------
			//配置验证选项
			var validate_options = {
				submitHandler : function(){
					var _submitHandler = options["onsubmit"] || options["submitHandler"];
					var form = this.currentForm;
					if(typeof( _submitHandler ) === "function"){ 
						 return function(form){
						 	_submitHandler(form);
						 };
					}else{
						return function(form){
							$(form).submit();
						}
					}
				}(),  
				rules : _rules,
				messages: _messages,
				errorElement : options["errorElement"] || "span", 
			}; 
			//验证未通过处理
			//function ( error, element ) {}
			if(typeof(options["errorPlacement"]) === "function"){
				validate_options["errorPlacement"] = options["errorPlacement"];
			}else{
				//默认配置
				validate_options["errorPlacement"] = function(error, element){
					alert(error);
					console.info(element);
				};
			}
			//highlight
			//function ( element, errorClass, validClass ) {}
			if(typeof options["highlight"] === "function"){
				validate_options["highlight"] = options["highlight"];
			}
			//unhighlight
			if (typeof options["unhighlight"] === "function") {
				validate_options["unhighlight"] = options["unhighlight"];
			}
			//验证通过处理
			//function ( label, element ){}
			if(typeof options["success"] === "function") validate_options["success"] = options["success"];
			//执行初始化
			console.log(validate_options);
			$(this).validate(validate_options); 
		}); 
	}); 
})(jQuery);